package com.itheima.controller;

import com.itheima.pojo.LoginInfor;
import com.itheima.pojo.Result;
import com.itheima.pojo.User;
import com.itheima.service.impl.UserServiceImpl;
import com.itheima.utils.ThreadHolder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("/login")
public class loginController {
    @Autowired
    private UserServiceImpl userService;
    @PostMapping
    public Result login(@RequestBody User user) {
        log.info("用户登录"+ user.getName());
        log.info("用户登录"+ user.getPassword());
        LoginInfor loginInfor =userService.login(user);
        if(loginInfor!=null){
            log.info("用户登录成功");
            ThreadHolder.set(loginInfor.getUser());
            return Result.success(loginInfor);
        }
        return Result.error("用户名或密码错误");
    }
}
